$("#login").on('click',function(){
  var username = $("input[name=username]").val();
  var password = $("input[name=password]").val();
  var postData = {'username':username,'password':password};
  var url = URL.login_url;
  var jump_url = URL.login_jump_url;
  $.post(url,postData,function(res){
      if(res.code == 200){
        return dialog.success(res.message,jump_url);
      }
      if(res.code == 400){
        return dialog.error(res.message);
      }
  },'json');
})

$("#register").on('click',function(){
  layer.open({
  type: 1,
  skin: 'layui-layer-rim', //加上边框
  area: ['600px', '600px'], //宽高
  content: '<div class="content_wrapper"><div class="container-fluid"><div class="row"><div class="col-md-12"><aside><header><i class="fa fa-fw fa-file"></i>用户注册</header><section><div class="container-fluid"><div class="row"><div class="col-md-12"><form class="form-horizontal" role="form" id="register-form"><div class="form-group"><label class="col-md-2 control-label">用户名</label><div class="col-md-3"><input type="text" class="form-control" placeholder="用户名" name="username"></div><p class="col-md-7 help-block" id="user-tip" style="color:red"></p></div><hr/><div class="form-group"><label class="col-md-2 control-label">密码</label><div class="col-md-3"><input type="password" class="form-control" placeholder="用户密码" name="password"></div></div><hr/><div class="form-group"><label class="col-md-2 control-label">重复密码</label><div class="col-md-3"><input type="password" class="form-control" placeholder="请再次输入用户密码" name="repassword"></div><p class="col-md-7 help-block" id="pwd-tip" style="color:red"></p></div><hr/><div class="form-group"><label class="col-md-2 control-label">用户邮箱</label><div class="col-md-3"><input type="email" class="form-control" placeholder="用户邮箱" name="email"></div></div><hr/><div class="form-group"><div class=" col-md-12"><button type="button" class="btn btn-primary" id="btn-register">提交</button></div></div></form></div></div></div></section></aside></div></div></div>'
});

$("#register-form input[name=username]").on('focusout',function(){
  var username = $("#register-form input[name=username]").val();
  var url = URL.checkUserExist;
  if(!username){
    return dialog.error("用户名不能为空");
  }else{
    $.post(url,{'username':username},function(res){
        if(res.code ==200){
          $("#user-tip").css('color','green').html(res.message);
        }
        if(res.code == 400){
          $("#user-tip").css('color','red').html(res.message);
        }
    },'json');
  }
})

$("#register-form input[name=repassword]").on('focusout',function(){
  var password = $("#register-form input[name=password]").val();
  var repassword = $("#register-form input[name=repassword]").val();
  if(password!=repassword){
    $("#pwd-tip").css('color','red').html("两次输入密码不一致，请重新输入！");
  }else{
    $("#pwd-tip").css('color','red').html("");
  }
})
$("#btn-register").on('click',function(){
  var username = $("#register-form input[name=username]").val();
  var password = $("#register-form input[name=password]").val();
  var repassword = $("#register-form input[name=repassword]").val();
  var email =  $("#register-form input[name=email]").val();
  if(!username||!password||!repassword||!email){
    return dialog.error("信息不完整，请补充完信息");
  }
  if(isEmail(email) == false){
    return dialog.error('请输入正确的邮箱格式');
  }
  var postData = {'username':username,'password':password,'email':email};
  var url = URL.register_url;
  var jump_url = URL.jump_url;
  $.post(url,postData,function(res){
      if(res.code == 200){
        return dialog.success(res.message,jump_url);
      }
      if(res.code == 400){
        return dialog.error(res.message);
      }
  },'json');
})
})
function isEmail(str){
var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
return reg.test(str);
}
